//
//  ZLPersonalCenterViewController.m
//  ZLExamination
//
//  Created by Zheng on 2017/8/9.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLPersonalCenterViewController.h"
#import "ZLExamRecordViewController.h"
#import "ZLPersonalCenterCell.h"
#import "ZLPersonalCenterHeaerView.h"
#import "MyCollectTableViewController.h"
#import "ZLBaseNavigationController.h"
#import "ZLPublicNoticeTableViewController.h"
#import "ZLPersonalCenterModel.h"
#import "ZLLoginViewController.h"
#import "sys/utsname.h"

static NSString *KZLPersonalCenterCell_ID = @"KZLPersonalCenterCell_ID";
static CGFloat kHeaderHeight = 150;

@interface ZLPersonalCenterViewController ()

@property (nonatomic, copy) NSArray<NSString *> *imageNames;
@property (nonatomic, strong) ZLPersonalCenterHeaerView *headerView;
@property (nonatomic, strong) UIButton *exitBtn;
@property (nonatomic, strong) ZLPersonalCenterModel *model;

@end

@implementation ZLPersonalCenterViewController

#pragma mark - 状态栏
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (UIStatusBarAnimation )preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationSlide;
}

- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _imageNames = @[@"",@"",@"",@""];
    
    self.myTableView.bounces = NO;
    [self.myTableView registerClass:[ZLPersonalCenterCell class] forCellReuseIdentifier:KZLPersonalCenterCell_ID];
    self.myTableView.tableHeaderView = self.headerView;
    [self.view addSubview:self.exitBtn];
    
    
    if (@available(iOS 11.0, *)) {
        self.myTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    [self setStatusBarBackgroundColor:kMY_BLUE_COLOR];
    
    [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.right.equalTo(self.view);
    }];
    
    if ([UIScreen mainScreen].bounds.size.height == 812) {
        [_exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(400);
            make.height.mas_equalTo(40);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.centerX.equalTo(self.view);
        }];
    } else {
        [_exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(370);
            make.height.mas_equalTo(40);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.centerX.equalTo(self.view);
        }];
    }
    
    
    [self loadData];
}

- (void)loadData {
    
    [[YCRequestDataService sharedManager] Post:@"user/user/student_info" parameters:nil success:^(id response) {
        
        if (response[@"error_msg"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            _model = [[ZLPersonalCenterModel alloc] initWithDataDic:response[@"response_data"][@"lists"]];
            
            [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_model] forKey:@"myInfo"];
            
            [self setHeaderViewData];
        }
    } failure:^{
        NSLog(@"网络或服务器出错");
    }];
}

- (void)setHeaderViewData {
    
    _headerView.nameLbl.text = _model.name;
    _headerView.cardLbl.text = _model.idocde;
    _headerView.division.text = _model.division_name;
    _headerView.idLbl.text = _model.user_id;
    _headerView.totalTimeLbl.text = [NSString stringWithFormat:@"总学时：%@",_model.credit];
    _headerView.studyTimeLbl.text = [NSString stringWithFormat:@"学习时长：%@ 小时",_model.read_length_time];
}


#pragma mark - subviews
- (ZLPersonalCenterHeaerView *)headerView {
    if (!_headerView) {
        _headerView = [[ZLPersonalCenterHeaerView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kHeaderHeight)];
    }
    return _headerView;
}

- (UIButton *)exitBtn {
    if (!_exitBtn) {
        _exitBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _exitBtn.backgroundColor = [UIColor whiteColor];
        [_exitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [_exitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_exitBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _exitBtn.layer.cornerRadius = 8;
    }
    return _exitBtn;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZLPersonalCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:KZLPersonalCenterCell_ID forIndexPath:indexPath];
    
    NSArray *titles = @[@"消息通知",@"考试记录",@"我的收藏"];
    NSArray *images = @[kIMAGE(@"消息"),kIMAGE(@"考试记录-1"),kIMAGE(@"收藏")];
    
    cell.contentLbl.text = titles[indexPath.row];
    cell.iconImgv.image = images[indexPath.row];
        
    return cell;
}

#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 57;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        kCX_LOG(@"消息通知");
        ZLPublicNoticeTableViewController *publicNoticeVC = [[ZLPublicNoticeTableViewController alloc] init];
        publicNoticeVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:publicNoticeVC animated:YES];
    } else if (indexPath.row == 1) {
        kCX_LOG(@"考试记录");
        ZLExamRecordViewController *erVC = [[ZLExamRecordViewController alloc] init];
        erVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:erVC animated:YES];
    } else {
        kCX_LOG(@"我的收藏");
        MyCollectTableViewController *myCollectVC = [[MyCollectTableViewController alloc] init];
        myCollectVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:myCollectVC animated:YES];
    }
}

#pragma mark - 点击事件
- (void)btnClickAction:(UIButton *)sender {
    kCX_LOG(@"退出登录");

    [[ZLInfoManager sharedManager] removeStudentAccessToken];

    ZLLoginViewController *loginVC = [[ZLLoginViewController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;
    
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
